home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / admin / dig-2.0 / dig-2 / dig.2.0 / res_init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-04  |  4.9 KB  |  155 lines

  1.  
  2. /*
  3.  * Copyright (c) 1985 Regents of the University of California.
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms are permitted
  7.  * provided that this notice is preserved and that due credit is given
  8.  * to the University of California at Berkeley. The name of the University
  9.  * may not be used to endorse or promote products derived from this
  10.  * software without specific prior written permission. This software
  11.  * is provided ``as is'' without express or implied warranty.
  12.  */
  13.  
  14. /*
  15. ** Modified for and distributed with 'dig' version 2.0 from 
  16. ** University of Southern California Information Sciences Institute
  17. ** (USC-ISI). 9/1/90
  18. */
  19.  
  20.  
  21. #if defined(LIBC_SCCS) && !defined(lint)
  22. static char sccsid[] = "@(#)res_init.c    6.8 (Berkeley) 3/7/88";
  23. #endif /* LIBC_SCCS and not lint */
  24.  
  25. #include "hfiles.h"
  26.  
  27. #include <sys/types.h>
  28. #include <sys/socket.h>
  29. #include <netinet/in.h>
  30. #include <stdio.h>
  31. #include NAMESERH
  32. #ifndef T_TXT
  33. #define T_TXT 16
  34. #endif T_TXT
  35. #include RESOLVH
  36.  
  37. /*
  38.  * Resolver configuration file. Contains the address of the
  39.  * inital name server to query and the default domain for
  40.  * non fully qualified domain names.
  41.  */
  42.  
  43. #ifndef    CONFFILE
  44. #define    CONFFILE    "/etc/resolv.conf"
  45. #endif
  46.  
  47. /*
  48.  * Resolver state default settings
  49.  */
  50.  
  51. struct state _res = {
  52.     RES_TIMEOUT,                   /* retransmition time interval */
  53.     4,                             /* number of times to retransmit */
  54.     RES_DEFAULT,        /* options flags */
  55.     1,                             /* number of name servers */
  56. };
  57.  
  58. /*
  59.  * Set up default settings.  If the configuration file exist, the values
  60.  * there will have precedence.  Otherwise, the server address is set to
  61.  * INADDR_ANY and the default domain name comes from the gethostname().
  62.  *
  63.  * The configuration file should only be used if you want to redefine your
  64.  * domain or run without a server on your machine.
  65.  *
  66.  * Return 0 if completes successfully, -1 on error
  67.  */
  68. res_init()
  69. {
  70.     register FILE *fp;
  71.     register char *cp, **pp;
  72.     char buf[BUFSIZ];
  73.     char *afile;
  74.     extern u_long inet_addr();
  75.     extern char *index();
  76.     extern char *strcpy(), *strncpy();
  77.     extern char *getenv();
  78.     int n = 0;    /* number of nameserver records read from file */
  79.  
  80.     _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
  81.     _res.nsaddr.sin_family = AF_INET;
  82.     _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
  83.     _res.nscount = 1;
  84.     _res.defdname[0] = '\0';
  85.  
  86.     if ((((afile=getenv("LOCALRES")) != NULL) && 
  87.      ((fp = fopen(afile,"r")) != NULL)) ||
  88.     ((fp = fopen(CONFFILE, "r")) != NULL)) {
  89.         /* read the config file */
  90.         while (fgets(buf, sizeof(buf), fp) != NULL) {
  91.             /* read default domain name */
  92.             if (!strncmp(buf, "domain", sizeof("domain") - 1)) {
  93.                 cp = buf + sizeof("domain") - 1;
  94.                 while (*cp == ' ' || *cp == '\t')
  95.                     cp++;
  96.                 if (*cp == '\0')
  97.                     continue;
  98.                 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
  99.                 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
  100.                 if ((cp = index(_res.defdname, '\n')) != NULL)
  101.                     *cp = '\0';
  102.                 continue;
  103.             }
  104.             /* read nameservers to query */
  105.             if (!strncmp(buf, "nameserver", 
  106.                sizeof("nameserver") - 1) && (n < MAXNS)) {
  107.                 cp = buf + sizeof("nameserver") - 1;
  108.                 while (*cp == ' ' || *cp == '\t')
  109.                     cp++;
  110.                 if (*cp == '\0')
  111.                     continue;
  112.                 _res.nsaddr_list[n].sin_addr.s_addr = inet_addr(cp);
  113.                 if (_res.nsaddr_list[n].sin_addr.s_addr == (unsigned)-1) 
  114.                     _res.nsaddr_list[n].sin_addr.s_addr = INADDR_ANY;
  115.                 _res.nsaddr_list[n].sin_family = AF_INET;
  116.                 _res.nsaddr_list[n].sin_port = htons(NAMESERVER_PORT);
  117.                 if ( ++n >= MAXNS) { 
  118.                     n = MAXNS;
  119. #ifdef DEBUG
  120.                     if ( _res.options & RES_DEBUG )
  121.                         printf(";; ** MAXNS reached, reading resolv.conf\n");
  122. #endif DEBUG
  123.                 }
  124.                 continue;
  125.             }
  126.       }
  127.         if ( n > 1 ) 
  128.             _res.nscount = n;
  129.         (void) fclose(fp);
  130.       }
  131.     if (_res.defdname[0] == 0) {
  132.         if (gethostname(buf, sizeof(_res.defdname)) == 0 &&
  133.            (cp = index(buf, '.')))
  134.              (void)strcpy(_res.defdname, cp + 1);
  135.     }
  136.  
  137.     /* Allow user to override the local domain definition */
  138.     if ((cp = getenv("LOCALDOMAIN")) != NULL)
  139.         (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
  140.  
  141.     /* find components of local domain that might be searched */
  142.     pp = _res.dnsrch;
  143.     *pp++ = _res.defdname;
  144.     for (cp = _res.defdname, n = 0; *cp; cp++)
  145.     if (*cp == '.')
  146.         n++;
  147.     cp = _res.defdname;
  148.     for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDNSRCH; n--) {
  149.     cp = index(cp, '.');
  150.     *pp++ = ++cp;
  151.     }
  152.     _res.options |= RES_INIT;
  153.     return(0);
  154. }
  155.